package com.sinocare.project.common.utils;
import org.apache.shiro.crypto.hash.Md5Hash;
import java.util.Random;

public class MD5 {
    /**
     * 添加时进行加密加盐，加散列
     * @param password
     * @param saltNum
     * @return
     */
    public static String md5AndSalt(String password,Integer saltNum){
        String salt1="1234567890qwertyuioplkjhgfdsazxcvbnmMNBVCXZASDFGHJKLOIPUYTREWQ";
        char[] chars = salt1.toCharArray();
        String salt="";
        for(int i=1;i<=8;i++){
            salt+=chars[new Random().nextInt(chars.length)];
        }
        Md5Hash md5Hash = new Md5Hash(password,salt,saltNum);
        String s = md5Hash.toHex() + "," + salt;
        System.out.println("生成："+s);
        return s;
    }

    /**
     * 登录时，把密码进行加密，加盐，加散列
     * @param password
     * @param salt
     * @param saltNum
     * @return
     */
    public static String md5Decode(String password,String salt,Integer saltNum){
        Md5Hash md5Hash = new Md5Hash(password,salt,saltNum);
        return md5Hash.toHex();
    }
}

